package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.ITripArea;
import com.gec.model.system.ITripSpace;
import com.gec.model.vo.ITripAreaQueryVo;
import com.gec.model.vo.ITripSpaceVo;
import com.gec.system.common.Result;
import com.gec.system.service.ITripAreaService;
import com.gec.system.service.ITripSpaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dafei
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/system/itrSpace")
@Api(tags="空间控制器")
@CrossOrigin
public class ITripSpaceController {
    @Autowired
    private ITripSpaceService iTripSpaceService;


    @ApiOperation("获取全部")

    @GetMapping("/findAll")
    public Result findAll()
    {
        List<ITripSpace> list = this.iTripSpaceService.list();
        return Result.ok(list);
    }

    //通过用户id获取用户发表的空间
    @GetMapping("/findByUserId")
    public Result findByUserId(Long userId)
    {
        List<ITripSpace> list = this.iTripSpaceService.getByUserId(userId);
        return Result.ok(list);
    }

    @ApiOperation("根据id去移除一个角色")
    // 测试删除
    @DeleteMapping("/removeSpace/{id}")
    public Result removeSpace(@PathVariable Long id)
    {
        boolean b = this.iTripSpaceService.removeById(id);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 分页和条件查询
    @GetMapping("/{page}/{limit}")
    public Result findSpaceByPageQuery(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      ITripSpaceVo iTripSpaceVo)
    {
        //1.创建分页对象
        IPage<ITripSpace> p1 = new Page<>(page,limit);
        //2.调用方法
        p1 =    this.iTripSpaceService.selectPage(p1,iTripSpaceVo);
        //3.返回
        return Result.ok(p1);
    }
    // 添加

//    @PostMapping("/addSpace")
//    public Result addSpace(@RequestBody ITripSpace iTripSpace)
//    {
//
//        boolean res = this.iTripSpaceService.save(iTripSpace);
//        if (res)
//        {
//            return Result.ok();
//        }
//        else
//        {
//            return Result.fail();
//        }
//    }
    // 修改

    @GetMapping("/findSpaceById/{id}")
    public Result findSpaceById(@PathVariable Long id)
    {
        ITripSpace iTripSpace = this.iTripSpaceService.getById(id);
        return Result.ok(iTripSpace);
    }



    // 实现修改
    @PostMapping("/updateSpace")
    public Result updateSpace(@RequestBody ITripSpace iTripSpace)
    {
        boolean b = this.iTripSpaceService.updateById(iTripSpace);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 批量删除
    @DeleteMapping("/removeSpaceByIds")
    public Result removeSpaceByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.iTripSpaceService.removeByIds(ids);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
}

